package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.zzyl.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;

/**
 * contractService业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-23
 */
@Service
@Slf4j
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询contract
     * 
     * @param id contract主键
     * @return contract
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询contract列表
     * 
     * @param contract contract
     * @return contract
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增contract
     * 
     * @param contract contract
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        return save(contract) ? 1 : 0;
    }

    /**
     * 修改contract
     * 
     * @param contract contract
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
        return updateById(contract) ? 1 : 0;
    }

    /**
     * 批量删除contract
     * 
     * @param ids 需要删除的contract主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除contract信息
     * 
     * @param id contract主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 更新合同状态
     */
    @Override
    public void updateContractStatus() {
        List<Contract> list = lambdaQuery().in(Contract::getStatus, 0, 1)
                .select(Contract::getId,Contract::getStartDate, Contract::getEndDate, Contract::getStatus).list();
        log.info("查找到合同数{}",  list.size());
        LocalDateTime now = LocalDateTime.now();
        // 确保需要更新的合同只有状态值上传，其他无关字段不更新，设置id作为更新条件
        ArrayList<Contract> updaeteList = new ArrayList<>();
        list.forEach(c->{
            Integer status = c.getStatus();
            if(status==0){
                if(c.getStartDate().isBefore(now)){
                    if(c.getEndDate().isBefore(now)){
                        Contract contract = new Contract();
                        contract.setId(c.getId());
                        contract.setStatus(3);
                        updaeteList.add(contract);
                    }
                    Contract contract = new Contract();
                    contract.setId(c.getId());
                    contract.setStatus(1);
                    updaeteList.add(contract);
                }
            }
            if(status==1){
                if(c.getEndDate().isBefore(now)){
                    Contract contract = new Contract();
                    contract.setId(c.getId());
                    contract.setStatus(2);
                    updaeteList.add(contract);
                }
            }
        });
        if(!CollectionUtils.isEmpty(updaeteList)){
            log.info("更新合同数{}",  updaeteList.size());
            updateBatchById(updaeteList);
        }
    }
}
